package ctgu.yao.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ctgu.yao.cloud.dto.ResultDTO;
import ctgu.yao.cloud.form.RuleForm;
import ctgu.yao.cloud.po.Admin;
import ctgu.yao.cloud.po.Resume;
import ctgu.yao.cloud.mapper.ResumeMapper;
import ctgu.yao.cloud.service.ResumeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ctgu.yao.cloud.form.RuleForm2;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Y
 * @since 2022-08-16
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {
    @Autowired
    private ResumeMapper resumeMapper;
    @Override
    public ResultDTO login(RuleForm ruleForm) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",ruleForm.getUsername());
        Resume resume = this.resumeMapper.selectOne(queryWrapper);
        ResultDTO resultDTO = new ResultDTO<>();
        if(resume == null){
            resultDTO.setCode(-1);
        }else{
            //判断密码是否正确
            if(!resume.getPassword().equals(ruleForm.getPassword())) {
                resultDTO.setCode(-2);
            }else {
                resultDTO.setCode(0);
                resultDTO.setData(resume);
            }
        }
        return  resultDTO;
    }
    //注册
    @Override
    public ResultDTO register(RuleForm2 ruleForm) {
        QueryWrapper<Resume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",ruleForm.getUsername());
        Resume resume = this.resumeMapper.selectOne(queryWrapper);
        ResultDTO resultDTO = new ResultDTO<>();
        //判断用户是否存在
        if(resume==null){
            resultDTO.setCode(0);
            Resume resume1=new Resume(ruleForm.getUsername(),ruleForm.getPassword(),ruleForm.getName(),
                    ruleForm.getPhone(),ruleForm.getEmail(),ruleForm.getSex(),ruleForm.getDate());
            //插入表
            System.out.println(resume1);
            this.resumeMapper.insert(resume1);
        }else{
            resultDTO.setCode(-1);
        }
        return resultDTO;
    }
}
